package com.wmh.baseservice.merchant.service.impl;

import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.merchant.pojo.MerchantApply;
import com.wmh.baseservice.merchant.mapper.MerchantApplyMapper;
import com.wmh.baseservice.merchant.pojo.MerchantInfo;
import com.wmh.baseservice.merchant.service.MerchantApplyService;
import com.wmh.baseservice.common.mybatisplus.methods.CommonServiceImpl;
import com.wmh.baseservice.merchant.service.MerchantInfoService;
import com.wmh.baseservice.system.pojo.SysMessage;
import com.wmh.baseservice.system.service.SysMessageService;
import com.wmh.baseservice.user.pojo.UsInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author wmh generator
 * @since 2022-01-07
 */
@Service
public class MerchantApplyServiceImpl extends CommonServiceImpl<MerchantApplyMapper, MerchantApply> implements MerchantApplyService {

    @Resource
    private MerchantApplyMapper merchantApplyMapper;
    @Resource
    private SysMessageService sysMessageService;
    @Resource
    private MerchantInfoService merchantInfoService;

    @Override
    public UsInfo getUsInfo(Long merchantId) {
        return merchantApplyMapper.getUsInfo(merchantId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(MerchantApply merchantApply) {
        SysMessage sysMessage = new SysMessage();
        sysMessage.setMerchantId(merchantApply.getMerchantId());
        sysMessage.setStatus(0);
        if (merchantApply.getStatus() == 1) {
            //同意
            sysMessage.setTitle("您的提现申请已通过审核");
            sysMessage.setContent("您的提现申请已通过审核,我们将会在两个工作日内转账到您的银行卡上，请注意查收");
        } else {
            //拒绝
            sysMessage.setTitle("很抱歉，您的提现申请被拒绝了，原因是:");
            sysMessage.setContent(merchantApply.getRefuseReason());
            //金额回滚
            MerchantInfo merchantInfo = merchantInfoService.getByIdForUpdate(merchantApply.getMerchantId());
            merchantInfo.setBalance(merchantInfo.getBalance().add(merchantApply.getMoney()));
            if (!merchantInfoService.updateById(merchantInfo)) {
                throw new ServiceException("提现审核: 金额回滚处错误");
            }
        }
        if (!sysMessageService.save(sysMessage)) {
            throw new ServiceException("提现审核: 发送消息错误,id:"+sysMessage.getId());
        }
        if (merchantApplyMapper.updateById(merchantApply) != 1) {
            throw new ServiceException("提现审核: 修改审核状态错误,id:"+merchantApply.getId());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void apply(MerchantApply merchantApply, MerchantInfo merchantInfo) {
        //金额扣除
        merchantInfo.setBalance(merchantInfo.getBalance().subtract(merchantApply.getMoney()));
        if (!merchantInfoService.updateById(merchantInfo)) {
            throw new ServiceException();
        }
        //提交申请
        if (merchantApplyMapper.insert(merchantApply)!=1) {
            throw new ServiceException();
        }
    }
}
